package cn.ly.gateway.core.datasource.connection;

import cn.ly.gateway.core.datasource.Connection;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.service.GenericService;

public class DubboConnection implements Connection {

    private final GenericService genericReference;

    public DubboConnection(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ReferenceConfig<GenericService> referenceConfig) {
        // 连接远程服务
        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
        bootstrap.application(applicationConfig)
                .registry(registryConfig)
                .reference(referenceConfig)
                .start();
        // 获取泛化接口
        ReferenceConfigCache referenceConfigCache = ReferenceConfigCache.getCache();
        this.genericReference = referenceConfigCache.get(referenceConfig);
    }

    @Override
    public Object execute(String method, String[] parameterTypes, String[] parameterName, Object[] args) {
        return genericReference.$invoke(method,parameterTypes,args);
    }
}
